classdef PRNCode < Signal

properties
	Codes;
	CodesFileName;
	ChipPeriod;
	NumberOfChips;
end % end of properties

methods
	function obj = PRNCode(Name)
		obj = obj@Signal(Name);
	end
end % end of methods

methods (Access = protected)

	function obj = loadCodes(obj)
		fid = fopen(obj.CodesFileName,'r');
		sv_id = 1; 
		count_sv = 1;
		chips = zeros(1, obj.NumberOfChips);
		while (count_sv > 0)

			tmp0 = fgets(fid);
			if ((~isempty(tmp0)) && (tmp0(1) ~= (-1)));
				tmp1 = str2num(tmp0);
				[m count_sv] = size(tmp0);
			else
				break;
			end
			if (tmp1 ~= sv_id)
				error('Reading PRN codes error')
			end
			
			fprintf(1, 'Loading PRN code from file %d ... \n', sv_id);

			count_hex_in_row = 1;
			chips_total = 0;
			while (count_hex_in_row > 0)
				tmp2 = fgets(fid);
				[m count_hex_in_row] = size(tmp2);
				if (count_hex_in_row > 0)
					count_hex_in_row = count_hex_in_row -1;	
					for hex_in_row_index=1:count_hex_in_row
						hex_in_dec = hex2dec(tmp2(hex_in_row_index));
						chips_in_dec = bitget(hex_in_dec, (4:(-1):1));
						if (obj.NumberOfChips < (chips_total+4))
							chips((1+chips_total):obj.NumberOfChips) = ...
								chips_in_dec(1:(obj.NumberOfChips - chips_total));
							chips_total = obj.NumberOfChips; 
							tmp2 = fgets(fid);
							[m count_hex_in_row] = size(tmp2);
							count_hex_in_row = count_hex_in_row -1;	
						else
							chips((1+chips_total):(chips_total+4)) = chips_in_dec;
							chips_total = chips_total +4;
						end
					end
				end
			end
			if (chips_total == obj.NumberOfChips)
				obj.Codes(sv_id, :) = chips;
				sv_id = sv_id +1;
			else
				error('Reading PRN codes error')
			end
		end

		fclose(fid);
	end

end % end of methods

end % end of class
